<?php

class Model_Member extends Model {

    function __construct() {
        $this->Lookup = getModel('Model_Lookup');
        $this->email_obj = getLibClass('itrialEmail');
        $this->userobj = getLibClass('UserCookies');
        $this->Points = getModel('Model_Points');
    }

    function h_getMember($h_param = array()) {

        $prama = array();
        $prama_key_0 = array('id', 'member_type', 'phone');
        if ($prama_key_0) {
            foreach ($prama_key_0 as $value) {
                $prama[$value] = (!empty($h_param[$value])) ? $h_param[$value] : 0;
            }
        }
        $prama_key_1 = array('page');
        if ($prama_key_1) {
            foreach ($prama_key_1 as $value) {
                $prama[$value] = (!empty($h_param[$value])) ? $h_param[$value] : 1;
            }
        }

        $prama['email'] = (!empty($h_param['email'])) ? $h_param['email'] : "";
        $prama['username'] = (!empty($h_param['username'])) ? $h_param['username'] : "";
        $prama['orderby'] = (!empty($h_param['orderby'])) ? $h_param['orderby'] : "id desc";
        $prama['size'] = (!empty($h_param['size'])) ? $h_param['size'] : 3;
        return $this->getMember($prama['id'], $prama['email'], $prama['username'], $prama['member_type'], $prama['orderby'], $prama['page'], $prama['size'], $prama['phone']);
    }

    /* 獲得會員資料
     * $id int 會員ID
     * $email int 會員email
     * $username string username
     * $member_type int 會員類型
     * $orderby		string	 	字段名 desc 或 字段名 asc
     * $page		int			當前頁數
     * $size		int			獲取個數
     * return		array		資料數組
     */

    function getMember($id = 0, $email = '', $username = '', $member_type = 0, $orderby = "id desc", $page = 1, $size = 3, $phone = 0) {
        $where = "status=1";
        if (intval($id))
            $where .=" and id=" . intval($id);
        if ($email)
            $where .=" and email='" . $email . "'";
        if ($phone)
            $where .=" and phone='" . $phone . "'";

        if ($username)
            $where .=" and username='" . $username . "'";
        if (intval($member_type))
            $where .=" and member_type=" . intval($member_type);

        $param = array();
        $param['field'] = "";
        $param['where'] = $where;
        $param['order'] = $orderby; //"id asc"
        $param['group'] = "";
        $param['limit'] = "";
        $param['pagesize'] = $size;
        $param['page'] = $page;
        $param['join'] = "";
        $param['on'] = "";
        $data = db_query_hash('member_imore', $param);

        if (intval($id) || $username) {
            $age = $this->Lookup->getLookup('agegroup');

            $agearr = array();
            if ($age['result']) {
                foreach ($age['result'] as $value) {
                    $agearr[$value['id']] = $value['name'];
                }
            }

            $result = array();
            if ($data['result']) {
                foreach ($data['result'] as $key => $value) {

                    $value['age'] = $agearr[getAgeGroup(getAge($value['birthday']))];
                    $value['agegroup'] = getAgeGroup(getAge($value['birthday']));
                    $result[$key] = $value;
                }
            }

            $data['result'] = $result;
        }

        return $data;
    }

    /* 獲得會員詳細資料
     * $mid int 會員ID
     * return		array		資料數組
     */

    function getMemberCondition($mid = 0, $phone = 0) {
        $where = "status=1";
        if (intval($mid))
            $where .=" and mid=" . intval($mid);
        if (intval($phone))
            $where .=" and phone=" . intval($phone);

        $param = array();
        $param['field'] = "";
        $param['where'] = $where;
        $param['order'] = ""; //"id asc"
        $param['group'] = "";
        $param['limit'] = 1;
        $param['pagesize'] = 1;
        $param['page'] = 1;
        $param['join'] = "";
        $param['on'] = "";
        $data = db_query_hash("member_condition", $param);
        return $data;
    }

    /* 查找會員資料

     * $keyword str 關鍵字
     * $orderby		string	 	字段名 desc 或 字段名 asc
     * $page		int			當前頁數
     * $size		int			獲取個數
     * return		array		資料數組
     */

    function searchMember($keyword = '', $orderby = "id desc", $page = 1, $size = 3) {

        $where = "status=1";
        if ($keyword)
            $where .=" and username like'%" . $keyword . "%'";

        $param = array();
        $param['field'] = "";
        $param['where'] = $where;
        $param['order'] = $orderby; //"id asc"
        $param['group'] = "";
        $param['limit'] = "";
        $param['pagesize'] = $size;
        $param['page'] = $page;
        $param['join'] = "";
        $param['on'] = "";
        $data = db_query_hash('member_imore', $param);
        return $data;
    }

    /* 獲得會員詳細資料
     * $mid int 會員ID
     * return		array		資料數組
     */

    function getMemberFacebook($mid = 0) {
        $where = "1";
        if (intval($mid))
            $where .=" and mid=" . intval($mid);

        $param = array();
        $param['field'] = "";
        $param['where'] = $where;
        $param['order'] = ""; //"id asc"
        $param['group'] = "";
        $param['limit'] = 1;
        $param['pagesize'] = 1;
        $param['page'] = 1;
        $param['join'] = "";
        $param['on'] = "";
        $data = db_query_hash("member_facebook", $param);
        return $data;
    }

    function go_curl($url, $postData = "") {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        if ($postData)
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $memberJsonData = curl_exec($ch);
        curl_close($ch);
        return $memberJsonData;
    }

    function postData($reg_p) {
        $aes = getLibClass('AES', $this->key);
        $postData = array();
        if (is_array($reg_p) || sizeof(get_object_vars($reg_p)) > 0) {
            foreach ($reg_p as $i => $v) {
                if ($i == 'password')
                    $v = $aes->encrypt($v);
                if ($i == 'opassword')
                    $v = $aes->encrypt($v);
                $v = urlencode($v);
                $postData [] = "{$i}={$v}";
            }
        }
        if (is_array($postData)) {
            $postData = join('&', $postData);
        }
        return $postData;
    }

    function login_succeed($memberData) {
        $itrial_member = $this->getMember($memberData['memberInfo']['id']);
        $facebook_member = $this->getMemberFacebook($memberData['memberInfo']['id']);
        if ($facebook_member['result'])
            $memberData['memberInfo']['facebook'] = $facebook_member['result'][0];

        if ($itrial_member['result']) {
            $post_data = array();
            $post_data['logintime'] = date('Y-m-d H:i:s');
            $condition = "id=" . mysql_escape_string($memberData['memberInfo']['id']);
            db_update('member_imore', $post_data, $condition);
            $memberData['memberInfo']['member_type'] = $itrial_member['result'][0]['member_type'];
        } else {
            $post_data = array();
            $post_data['id'] = $memberData['memberInfo']['id'];
            $post_data['photo'] = $memberData['memberInfo']['photo'];
            $post_data['username'] = $memberData['memberInfo']['username'];
            $post_data['email'] = $memberData['memberInfo']['email'];
            $post_data['email_status'] = $memberData['memberInfo']['email_status'];
            $post_data['addtime'] = date('Y-m-d H:i:s');
            $post_data['logintime'] = date('Y-m-d H:i:s');
            $post_data['member_type'] = 0;
            db_add('member_imore', $post_data);
            $memberData['memberInfo']['member_type'] = 0;
            $this->Points->points_record($memberData['memberInfo']['id'], 'reg');
            sleep(1);
        }

        $member = $this->getMember($memberData['memberInfo']['id']);
        if ($member['result'])
            $memberData['memberInfo']['agegroup'] = $member['result'][0]['agegroup'];
        $this->userobj->set('memberData', $memberData['memberInfo']);
    }

    function set_newSession($userid) {
        $member = array();
        $itrial_member = $this->getMember($userid);
        if ($itrial_member['result'])
            $member = $itrial_member['result'][0];
        $facebook_member = $this->getMemberFacebook($userid);

        $memberDataNew = array();
        $memberDataNew['id'] = $member['id'];
        $memberDataNew['email_status'] = $member['email_status'];
        $memberDataNew['email'] = $member['email'];
        $memberDataNew['photo'] = $member['photo'];
        $memberDataNew['email_receive'] = $member['email_receive'];
        $memberDataNew['username'] = $member['username'];
        $memberDataNew['member_type'] = $member['member_type'];
        if ($facebook_member['result'])
            $memberDataNew['facebook'] = $facebook_member['result'][0];

        $this->userobj->set('memberData', $memberDataNew);
    }

    function resend_email($email, $memberData, $global_url) {

        if (($email && $email <> $memberData['email'])) {

            $url = $global_url . $this->selfurl . "?action=activation&membercode=" . md5($this->activationKey . $memberData['id'] . $memberData['username'] . $email) . "&mid=" . $memberData['id'];

            $this->email_obj->send($memberData['id'], "become_member", array('username' => $memberData['username'], 'emailUrl' => $url));
        }
    }

    function check_imore_member($email, $password) {
        $url = IMOREDOMAIN . "memberApi.php?action=ajax_login&username=" . urlencode($email) . "&password=" . urlencode($password);

        $memberData = json_decode($this->go_curl($url), 1);
        return $memberData;
    }

    function check_fnb_member($email, $password) {
        $url = IMOREDOMAIN . "memberApi.php?action=ajax_fnb_login&username=" . urlencode($email) . "&password=" . urlencode($password);

        $memberData = json_decode($this->go_curl($url), 1);


        return $memberData;
    }

}

?>